<?php

class cMyReport extends BaseController{
	
	public function index(){
	
		$this->loadView("vMenu");
		$this->loadView("vMyReport");
		
		echo JsUtils::postFormAndBindTo("#deconnexion", "click", "cAccueil/deconnexion", "menu","#menu");
		echo JsUtils::postFormAndBindTo("#deconnexion", "click", "cAccueil/deconnexion", "menu","#menu");
		
		if(isset($_SESSION['id'])){
	
			$allReport = DAO::getAll("Report", "idUtilisateur=".$_SESSION['id']);
			
			Gui::debutTable();
			
			foreach($allReport as $report){
				//affiche mes reports
				Gui::getExists($report);
			}
			
			Gui::finTable();
			
		}

		echo JsUtils::postAndBindTo(".ligne","click","cMyReport/detail","{idReport:$(this).attr('value')}","#r");
		
	}
	

	
	public function detail(){
		
		if(isset($_POST['idReport'])){
			$idReport = $_POST['idReport'];
		
			Gui::message($idReport);
		}
	
		echo JsUtils::postAndBindTo(".resolu","click","cMyReport/resolvedReport","{messageResolu:$(this).attr('name')}","#r");
		echo JsUtils::postAndBindTo("#addMessageReport","click","cMyReport/insertResponse","{message:$('#message').val(),idReport:$('#idReport').attr('value')}","#r");
	}
	
	
	
	public function insertResponse(){
		
		if(!empty($_SESSION['id'])){
			$id = $_SESSION['id'];
		}
		
		$message = $_POST['message'];
		
		if(!empty($message)){
			
			$idReport = $_POST['idReport'];
		
			$date = date("Y-m-d");
		
			$allMessage = DAO::getAll("Message");
				
			foreach ($allMessage as $messages){
		
				if($messages->getIdReport()==$idReport){
					$aMessage=true;
						
				}
			}
		
		
			$messageInsert = new Message($idReport,$id,$message,$date);
			$ret = DAO::insert($messageInsert);
		
			$reports = DAO::getAll("Report");
		
		
			foreach ($reports as $report){
		
				if($report->getId() == $idReport && isset($aMessage) == true && $report->getIdStatut()=="5"){
						
					$report1 = new Report($report->getId(),"5",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$report->getReponseOfficielle());
					DAO::update($report1);
						
				}
				elseif($report->getId() == $idReport && isset($aMessage) == true){
				
					$report1 = new Report($report->getId(),"3",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$report->getReponseOfficielle());
					$e =DAO::update($report1);
		
				}
				elseif ($report->getId() == $idReport && isset($aMessage) != true){
					
					$report1 = new Report($report->getId(),"1",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$report->getReponseOfficielle());
					DAO::update($report1);
				}
			}
			echo "<div id='myReport'>";
			Gui::message($idReport);
			echo "</div>";
			echo JsUtils::postAndBindTo("#addMessageReport","click","cMyReport/insertResponse","{message:$('#message').val(),idReport:$('#idReport').attr('value')}","#myReport");
		
		}
		else{
			echo JsUtils::execute('alert("Veuillez saisir un message")');
		}
	}
	
	
	public function resolvedReport(){
		$messageResolu = explode("+",$_POST['messageResolu']);
	
		$message = DAO::getAll("Message","id=".$messageResolu[1]);
		$reports = DAO::getAll("Report");
	
		foreach ($reports as $report){
			foreach ($message as $aMessage){
				if($report->getId() == $messageResolu[0]){
						
					$report1 = new Report($report->getId(),"5",$report->getIdUtilisateur(),$report->getCode(),$report->getLibelle(),$report->getDescription(),$report->getCreationDate(),$report->getUserAction(),$report->getSystemMessage(),$aMessage->getContenu());
						
					DAO::update($report1);
				}
			}
		}
	
	}
	

}